package com.augurit.smartform.converter;

import java.util.ArrayList;
import java.util.List;

import com.augurit.smartform.display.PageElement;
import com.augurit.smartform.entity.SmPage;
import com.augurit.smartform.mgmt.SmartPageTreeNode;
import com.augurit.smartform.web.parser.form.SmPageForm;

public class PageConverter {
	public static PageElement convertToPage(SmPage smPage){
		PageElement pageElement=null;
		if(smPage!=null){
			pageElement=new PageElement();
			pageElement.setName(smPage.getName());
			pageElement.setCnName(smPage.getCnName());
			pageElement.setClientJavascript(smPage.getClientJavascript());
			pageElement.setServerJavascript(smPage.getServerJavascript());
			pageElement.setSourceCss(smPage.getSourceCss());
			pageElement.setSourceName(smPage.getSourceName());
			pageElement.setSourcePath(smPage.getSourcePath());
			pageElement.setJspName(smPage.getJspName());
			pageElement.setJspPath(smPage.getJspPath());
			pageElement.setJspContent(smPage.getJspContent());
			pageElement.setPageCode(smPage.getPageCode());
			pageElement.setTemplateId(smPage.getTemplateId());
			pageElement.setPageId(smPage.getPageId());
			
			pageElement.setSourceFtlStr(smPage.getSourceFtl());
			pageElement.setSourceHtmlStr(smPage.getSourceHtml());
			pageElement.setSourceXmlStr(smPage.getSourceXml());
		}
		return pageElement;
	}
	
	public static SmPage convertToSmPage(PageElement page){
		SmPage smPage=null;
		if(page!=null){
			smPage=new SmPage();
			smPage.setName(page.getName());
			smPage.setCnName(page.getCnName());
			smPage.setClientJavascript(page.getClientJavascript());
			smPage.setServerJavascript(page.getServerJavascript());
			smPage.setSourceCss(page.getSourceCss());
			smPage.setSourceName(page.getSourceName());
			smPage.setSourcePath(page.getSourcePath());
			smPage.setJspName(page.getJspName());
			smPage.setJspPath(page.getJspPath());
			smPage.setJspContent(page.getJspContent());
			smPage.setPageCode(page.getPageCode());
			smPage.setTemplateId(page.getTemplateId());
			smPage.setPageId(page.getPageId());
			
			smPage.setSourceFtl(page.getSourceFtlStr());
			smPage.setSourceHtml(page.getSourceHtmlStr());
			smPage.setSourceXml(page.getSourceXmlStr());
		}
		return smPage;
	}
	
	public static void convertToSmPage(PageElement page,SmPage smPage){
		if(page!=null&&smPage!=null){
			smPage.setName(page.getName());
			smPage.setCnName(page.getCnName());
			smPage.setClientJavascript(page.getClientJavascript());
			smPage.setServerJavascript(page.getServerJavascript());
			smPage.setSourceCss(page.getSourceCss());
			smPage.setSourceName(page.getSourceName());
			smPage.setSourcePath(page.getSourcePath());
			smPage.setJspName(page.getJspName());
			smPage.setJspPath(page.getJspPath());
			smPage.setJspContent(page.getJspContent());
			smPage.setPageCode(page.getPageCode());
			smPage.setTemplateId(page.getTemplateId());
			smPage.setPageId(page.getPageId());
			
			smPage.setSourceFtl(page.getSourceFtlStr());
			smPage.setSourceHtml(page.getSourceHtmlStr());
			smPage.setSourceXml(page.getSourceXmlStr());
		}
	}
	
	public static SmPageForm convertToPageForm(SmPage smPage){
		SmPageForm smPageForm=null;
		if(smPage!=null){
			smPageForm=new SmPageForm();
			smPageForm.setId(smPage.getId());
			smPageForm.setCnName(smPage.getCnName());
			smPageForm.setJspPath(smPage.getJspPath());
			smPageForm.setName(smPage.getName());
			smPageForm.setPageCode(smPage.getPageCode());
			smPageForm.setTemplateId(smPage.getTemplateId());
			smPageForm.setPageDirId(smPage.getPageDirId());
			if(smPage.getTemplateId()!=null){
				smPageForm.setFormType("流程表单");
			}else{
				smPageForm.setFormType("普通表单");
			}
		}
		return smPageForm;
	}
	
	public static List<SmPageForm> convertToFormList(List<SmPage> smPageList){
		List<SmPageForm> smPageForms=new ArrayList<SmPageForm>();
		if(smPageList!=null&&smPageList.size()>0){
			for(SmPage smPage : smPageList){
				smPageForms.add(convertToPageForm(smPage));
			}
		}
		return smPageForms;
	}
	
	/**
	 * 将Form对象列表转换为ExtTreeNode对象列表
	 * @param form
	 * @return
	 */
	public static SmartPageTreeNode convertPageDirToTreeNode(SmPageForm form){
		if(form != null){
			SmartPageTreeNode node = new SmartPageTreeNode();
			node.setId(SmartPageTreeNode.getKey(SmPage.class.getSimpleName(), form.getId()));
			node.setText(form.getCnName());
			node.setQtip(form.getName());
			node.setIconCls(SmartPageTreeNode.CLS_APP);
			node.setLeaf(true);
			return node;
		}else{
			return null;
		}	
	}
	
	/**
	 * 将Form对象列表转换为ExtTreeNode对象列表
	 * @param list
	 * @return
	 */
	public static List<SmartPageTreeNode> convertPageDirToTreeNode(List<SmPageForm> list){
		List<SmartPageTreeNode> result = new ArrayList<SmartPageTreeNode>();
		if(list != null && list.size() > 0){
			for(SmPageForm page : list)
				result.add(PageConverter.convertPageDirToTreeNode(page));
		}
		return result;
	}
}
